<?php

use think\Db;

$objToFiles = [ //obj => file
    "task_base.json" => "task.php", //task_data.xlsx
    "hero_desc.json" => "hero.php", //hero_data.xlsx
    "assets_base.json" => "data_economy.php", //assets_data.xlsx
    "item_base.json" => "data_item.php", //item_data.xlsx
    "recharge_sub_type_1.json" => "recharge_sub_type_1.php", //recharge_data.xlsx
    "recharge_sub_type_2.json" => "recharge_sub_type_2.php",
    "recharge_sub_type_4.json" => "recharge_sub_type_4.php",
    "recharge_sub_type_5.json" => "recharge_sub_type_5.php",
    "recharge_sub_type_6.json" => "recharge_sub_type_6.php",
    "recharge_sub_type_7.json" => "recharge_sub_type_7.php",
    "recharge_sub_type_8.json" => "recharge_sub_type_8.php",
    "recharge_sub_type_9.json" => "recharge_sub_type_9.php",
    "recharge_sub_type_10.json" => "recharge_sub_type_10.php",
    "recharge_sub_type_11.json" => "recharge_sub_type_11.php",
    "recharge_sub_type_12.json" => "recharge_sub_type_12.php",
    "recharge_type.json" => "recharge_type.php",
    "origin.json" => "origin.php",
];


foreach ($objToFiles as $obj => $file) {
    if (!file_exists($obj)) { //主文件不存在则不处理
        print("obj continue" . $obj . "\n");
        continue;
    }
    print("开始处理" . $obj . "\n");
    $c = file_get_contents('./' . $obj);
    if ($obj == "origin.json") {
        $t = json_decode($c, true);
        $arrayDict = [];
        if (empty($t)) {
            continue;
        }
        foreach ($t as $key => $value) {
            $arrayDict[$key] = $value["desc"];
        }
        $s = "<?php return " . var_export($arrayDict, true) . ";";
    } elseif ($obj == "task_base.json") {
        $t = json_decode($c, true);
        $arrayDict = [];
        if (empty($t)) {
            continue;
        }
        foreach ($t as $key => $value) {
            $arrayDict[$key] = $value["task_des"];
        }
        $s = "<?php return " . var_export($arrayDict, true) . ";";
    } else {
        $s = "<?php return " . var_export(json_decode($c, true), true) . ";";
    }
    file_put_contents('./' . $file, $s);
}

ProcessItem();

function ProcessItem()
{
    print("开始处理 item extra\n");
    $content = include_once("data_item.php");
//需要入库的字段
    $fieldArr = array("code", "name", "pic", "type", "sub_type", "req_career", "req_sex", "req_lv", "req_round", "overlay", "quality", "desc");
    $addArr = $info = $detailInfo = array();


//循环拿到的数组对象
    foreach ($content as $row) {
        $i++;
        $arr = array();
        if (!isset($row["template_id"])) continue;
        //把对象转成数组形式，方便使用
        $row["code"] = $row["template_id"];
        //获取入库字段的值
        foreach ($fieldArr as $key) {
            $arr[$key] = (!isset($row[$key]) || empty($row[$key])) ? "" : $row[$key];
        }
        //组装多维数组以便批量入库
        $addArr[] = $arr;

        //组装code对应的名称，写成文件缓存
        $info[$arr["code"]] = $arr["name"];
        $detailInfo[$row["code"]] = array(
            "name" => $arr["name"],
            "type" => $arr["type"],
            "req_lv" => (int)$arr["req_lv"],
//                "req_round" => (int)$row["req_round"],
        );
    }

//写入文件缓存
    $s1 = "<?php return " . var_export($info, true) . ";";
    $s2 = "<?php return " . var_export($detailInfo, true) . ";";
    file_put_contents("item.php", $s1);
    file_put_contents("itemDetail.php", $s2);
}


